package com.zip.z4player;

import android.content.DialogInterface;
import android.graphics.Color;
import android.os.Environment;
import android.support.annotation.IdRes;
import android.support.design.widget.NavigationView;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.Toast;

import com.zip.filemanager.FileFragment;
import com.zip.player.PlayerFragment;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity implements FileFragment.FragmentCallBackValue {
    final String Tag = "MainActivity_Tag";

    private FileFragment mFileFragment=new FileFragment();
    private PlayerFragment[] mPlayerFragment = new PlayerFragment[4];
    private FrameLayout[] mFrameLayout = new FrameLayout[4];
    private CheckBox[] mCheckBox = new CheckBox[4];

    private LinearLayout ll_Framelayout0;
    private LinearLayout ll_Framelayout1;


    //region 滑动侧边栏
    DrawerLayout mDrawerLayout;
    NavigationView mNavigationView;
    //endregion

    List<Integer> LastID = new ArrayList<>();

    @IdRes
    int[] fl_content = {R.id.fl_content0, R.id.fl_content1, R.id.fl_content2, R.id.fl_content3};

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
        mDrawerLayout.setScrimColor(Color.TRANSPARENT);

        ll_Framelayout0 = (LinearLayout) findViewById(R.id.ll_Framelayout0);
        ll_Framelayout1 = (LinearLayout) findViewById(R.id.ll_Framelayout1);

        FragmentManager fragmentManager = getSupportFragmentManager();
        FragmentTransaction transaction = fragmentManager.beginTransaction();
        for (int i = 0; i < 4; i++) {
            mFrameLayout[i] = (FrameLayout) findViewById(fl_content[i]);
        }

        Bundle bundle = new Bundle();
        bundle.putString("path","/storage");
        mFileFragment.setArguments(bundle);
        transaction.add(R.id.fl_side,mFileFragment);
        transaction.commit();

        mCheckBox[0] = (CheckBox) findViewById(R.id.chk_window0);
        mCheckBox[1] = (CheckBox) findViewById(R.id.chk_window1);
        mCheckBox[2] = (CheckBox) findViewById(R.id.chk_window2);
        mCheckBox[3] = (CheckBox) findViewById(R.id.chk_window3);
        for (CheckBox chk : mCheckBox) {
            chk.setOnCheckedChangeListener(checkedChangeListener);
        }

       // mCheckBox[0].setText( strfile);
    }

    @Override
    public void onBackPressed() {
        if (mDrawerLayout.isDrawerOpen(GravityCompat.START)) {
            mDrawerLayout.closeDrawer(GravityCompat.START);
        } else if (LastID.size() > 0) {
            int tempID = LastID.get(LastID.size() - 1);
        } else {
            super.onBackPressed();
        }
    }


    //region 侧边栏事件
    CompoundButton.OnCheckedChangeListener checkedChangeListener = new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
            if (!mCheckBox[0].isChecked() && !mCheckBox[1].isChecked()
                    && !mCheckBox[2].isChecked() && !mCheckBox[3].isChecked()) {
                compoundButton.setChecked(!b);
                Toast.makeText(MainActivity.this, "至少要打开一个窗口!", Toast.LENGTH_SHORT).show();
            }
            for (int i = 0; i < mCheckBox.length; i++) {
                FrameLayoutShow(i, mCheckBox[i].isChecked());
            }

        }
    };

    //endregion
    @Override
    public void FrameLayoutShow(int id, boolean shown) {
        // TODO Auto-generated method stub
        try {
            mFrameLayout[id].setVisibility(shown ? View.VISIBLE : View.GONE);

            if (mFrameLayout[0].getVisibility() == View.GONE && mFrameLayout[1].getVisibility() == View.GONE) {
                ll_Framelayout0.setVisibility(View.GONE);
            } else {
                ll_Framelayout0.setVisibility(View.VISIBLE);
            }

            if (mFrameLayout[2].getVisibility() == View.GONE && mFrameLayout[3].getVisibility() == View.GONE) {
                ll_Framelayout1.setVisibility(View.GONE);
            } else {
                ll_Framelayout1.setVisibility(View.VISIBLE);
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void ReturnVideoPath(final File file) {
        Log.d(Tag, "文件:" + file.getPath());
//        if (mDrawerLayout.isDrawerOpen(GravityCompat.START)) {
//            mDrawerLayout.closeDrawer(GravityCompat.START);
//        }
        if (file.exists()) {
            String[] arrWin = {"窗口1", "窗口2", "窗口3", "窗口4"};
            AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
            builder.setTitle("选择播放窗口:");
            builder.setSingleChoiceItems(arrWin, -1, new DialogInterface.OnClickListener() {// 第二个参数是设置默认选中哪一项-1代表默认都不选
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    int id = which;

                    FragmentManager fragmentManager = getSupportFragmentManager();
                    FragmentTransaction transaction = fragmentManager.beginTransaction();


                    if (mPlayerFragment[id] != null && mPlayerFragment[id].isAdded())
                        transaction.remove(mPlayerFragment[id]);
                    mPlayerFragment[id] = new PlayerFragment(file.getPath());
                    transaction.add(fl_content[id], mPlayerFragment[id]);
                    transaction.commit();
                    LastID.add(id);
                    //FrameLayoutShow(id,true); //mCheckBox checked Change监听事件为FrameLayoutShow
                    mCheckBox[id].setChecked(true);
                    dialog.dismiss();
                }
            });
            AlertDialog dialog = builder.create();
            dialog.show();
            dialog.setCanceledOnTouchOutside(true);// dialog弹出后，点击界面其他部分dialog消失


        }
    }

}
